package com.njegovanobradovic.icet.service.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.njegovanobradovic.icet.metamodel.Chat;
import com.njegovanobradovic.icet.metamodel.User;
import com.njegovanobradovic.icet.service.GetUserChats;

public class GetUserChatsImpl extends HibernateDaoSupport implements
		GetUserChats {
	private User user;

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	protected Session getManager() {
		return getSessionFactory().getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<Chat> execute() {
		Criteria criteria = getManager().createCriteria(User.class);
		List<User> list = criteria.list();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getUsername().equals(getUser().getUsername())) {
				System.out.println("\n*\n*\n*\nChats of user "
						+ list.get(i).getName() + ": " + list.get(i).getChats()
						+ "\n*\n*\n*");
				return list.get(i).getChats();
			}
		}
		return null;
	}
}
